import { isEmpty } from "@/utils"
import { FormProps, SearchFormProps } from "../types"
import { FormItemRule, FormRules } from "naive-ui"
import { defaultFormItemProps, defaultFormProps } from "./native-props"
import { hasPermission, isVisible } from "@/components/hooks"

/**
 * 属性转换
 */
export function useFormProps(props: Ref<FormProps>) {
    /**
     * 表单属性
     */
    const bindFormProps = computed(() => {
        const { size, inline, labelWidth, labelAlign, labelPlacement, labelShow, showFeedback, items = [] } = unref(props)
        // 合并校验规则
        const rules: FormRules = {}
        items.filter(item => !isEmpty(item.rules)).forEach(item => (rules[item.property] = item.rules as FormItemRule | FormItemRule[]))
        return {
            ...defaultFormProps,
            size,
            inline,
            labelWidth,
            labelAlign,
            labelPlacement,
            showLabel: labelShow,
            showFeedback,
            rules
        }
    })

    /**
     * 是否使用Grid排版
     */
    const useGrid = computed(() => unref(props).useGrid)

    /**
     * Grid属性
     */
    const bindGridProps = computed(() => {
        const { cols, gap, search } = unref(props)
        let collapsedRows = undefined
        if (search) {
            const searchFormProps = (search === true ? {} : search) as SearchFormProps
            collapsedRows = searchFormProps.advanceRows
        }
        return { cols, xGap: gap, collapsedRows }
    })

    const formItems = computed(() => {
        const { items = [] } = unref(props)
        return items
            .filter(item => isVisible(item) && hasPermission(item))
            .map(item => {
                const {
                    offset,
                    span,
                    path,
                    property,
                    label,
                    labelAlign,
                    labelPlacement,
                    labelWidth,
                    labelShow,
                    size,
                    showFeedback,
                    type,
                    disabled,
                    clearable,
                    placeholder,
                    code,
                    nativeProps = {}
                } = item
                // Grid 属性
                const bindGridProps = { offset, span }
                // FormItem属性
                const bindItemProps = {
                    ...defaultFormItemProps,
                    label,
                    labelAlign,
                    labelPlacement,
                    labelWidth,
                    showLabel: labelShow,
                    path: path ?? property,
                    size,
                    showFeedback
                }
                // Input属性
                const bindInputProps =
                    type === "hidden"
                        ? {}
                        : {
                              type,
                              property,
                              clearable,
                              placeholder,
                              code,
                              disabled,
                              nativeProps
                          }
                return { ...item, bindGridProps, bindItemProps, bindInputProps }
            })
    })

    return { bindFormProps, useGrid, bindGridProps, formItems }
}
